package cm.stu.dao;

import cm.stu.pojo.Person;
import cm.stu.util.ConnectionFactory;
import com.sun.crypto.provider.HmacSHA1;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ManageDaoImpl implements ManageDao {
    @Override
    public List<Person> getAllPerson() {
        String sql = "select * from person where userIdentify!=2";
        return Deal.getAllPerson(sql);
    }

    @Override
    public List<Person> getSearch(String matchText) {
        String sql = "select * from person where userIdentify!=2 and (userAccount = '"+matchText+"' or userName like '%"+matchText+"%' or userIdCard = '"+matchText+"')";
        return Deal.getAllPerson(sql);
    }

    @Override
    public void addPerson(Person newPerson) {
        String sql = "insert into person values('"+newPerson.getUserAccount()+"','"+newPerson.getUserName()+"','"+newPerson.getUserSex()+"','"+newPerson.getUserBirthday()+"','"+newPerson.getUserIdCard()+"','"+newPerson.getUserPassword()+"',"+newPerson.getUserIdentify()+",'"+newPerson.getUserOtherName()+"')" ;
        Deal.deal(sql);
    }

    @Override
    public void sureUpPassword(String userAccount, String userPassword) {
        String sql = "update person set userPassword = '"+userPassword+"' where userAccount='"+userAccount+"'";
        Deal.deal(sql);
    }

    @Override
    public void delThisPerson(String delAccount, String userIdentify) {
        if (Integer.parseInt(userIdentify)==0){
            //删除学生
            String sql = "DELETE FROM userteacher WHERE 'userAccount'='"+delAccount+"'";
            Deal.deal(sql);
            sql = "DELETE FROM studentTask WHERE studentAccount = '"+delAccount+"'";
            Deal.deal(sql);

        }else{
            //删除老师
            String sql = "DELETE FROM `studentTask` WHERE taskAccount IN (SELECT `taskAccount` FROM `task` WHERE `teacherAccount` = '" + delAccount + "')";
            Deal.deal(sql);
            sql = "DELETE FROM task WHERE teacherAccount = '"+delAccount+"'";
            Deal.deal(sql);
            sql = "DELETE FROM userteacher WHERE teacherAccount = '"+delAccount+"'";
            Deal.deal(sql);
        }
        String sql = "DELETE FROM person WHERE userAccount = '"+delAccount+"'";
        Deal.deal(sql);
    }

    @Override
    public void zhuCePerson(Person newPerson) {
        String sql = "insert into person values('"+newPerson.getUserAccount()+"','"+newPerson.getUserName()+"','"+newPerson.getUserSex()+"','"+newPerson.getUserBirthday()+"','"+newPerson.getUserIdCard()+"','"+newPerson.getUserPassword()+"',"+newPerson.getUserIdentify()+",'"+newPerson.getUserOtherName()+"')" ;
        Deal.deal(sql);
    }

    @Override
    public void updatePerson(Person updatedPerson) {
        String sql = "UPDATE person SET userName = ?, userSex = ?, userBirthday = ?, userIdCard = ?, userOtherName = ? WHERE userAccount = ?";
        try (Connection conn = ConnectionFactory.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, updatedPerson.getUserName());
            pstmt.setString(2, updatedPerson.getUserSex());
            pstmt.setString(3, updatedPerson.getUserBirthday());
            pstmt.setString(4, updatedPerson.getUserIdCard());
            pstmt.setString(5, updatedPerson.getUserOtherName());
            pstmt.setString(6, updatedPerson.getUserAccount());

            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            // 处理异常，例如记录日志或抛出运行时异常
            throw new RuntimeException("更新用户信息时发生错误", e);
        }
    }
}
